<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Teacher extends Admin_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('myclass');
        $this->load->model('teacher_m');
        /** 检查登陆 */
        if (!$this->auth->is_admin()) {
            show_message('非管理员或未登录', site_url('admin/login/do_login'));
        }
    }

    public function index($page = 1) {
        $data['title'] = '教师风采管理';
        //分页
        $limit = 20;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('admin/teacher/index/');
        $config['total_rows'] = $this->db->count_all('teacher');
        $config['per_page'] = $limit;
        $config['prev_link'] = '&larr;';
        $config['prev_tag_open'] = '<li class=\'prev\'>';
        $config['prev_tag_close'] = '</li';
        $config['cur_tag_open'] = '<li class=\'active\'><span>';
        $config['cur_tag_close'] = '</span></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['next_link'] = '&rarr;';
        $config['next_tag_open'] = '<li class=\'next\'>';
        $config['next_tag_close'] = '</li>';
        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li class=\'first\'>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = '尾页';
        $config['last_tag_open'] = '<li class=\'last\'>';
        $config['last_tag_close'] = '</li>';
        $config['num_links'] = 10;
        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $start = ($page - 1) * $limit;
        $data['pagination'] = $this->pagination->create_links();
        $where = array();
        $oredrBy = array(
            'o' => 'asc',
            'status' => 'desc',
        );
        $data['teacher'] = $this->teacher_m->getListByCondition("teacher", $where, $oredrBy, $start, $limit);
        $this->load->view('teacher', $data);
    }

    public function del($id) {
        $data['title'] = '删除教师风采';
        //删除教师风采
        if ($this->teacher_m->del_teacher($id)) {
            show_message('删除教师风采成功！', site_url('admin/teacher'), 1);
        }
    }

    public function edit($id) {
        $data['title'] = '修改教师风采';
        if ($_POST) {
            $str = array(
                'title' => $this->input->post('title'),
                'pic' => $this->input->post('pic'),
                'o' => $this->input->post('o'),
                'zhicheng' => $this->input->post('zhicheng'),
                'content' => $this->input->post('content'),
                'jianjie' => $this->input->post('jianjie'),
                'status' => $this->input->post('status')
            );
//            var_dump($str);
//            die;
            if ($id) {
                if ($this->teacher_m->update_teacher($id, $str)) {
                    show_message('修改教师风采成功！', site_url('admin/teacher'), 1);
                }
            } else {
                $str['addtime'] = time();
                if ($this->teacher_m->add($str)) {
                    show_message('新增教师风采成功！', site_url('admin/teacher'), 1);
                }
            }
        }
        $where = array('id' => $id);
        $oredrBy = array(
            'o' => 'asc',
            'status' => 'desc',
        );
        $data['teacher'] = $this->teacher_m->getListByCondition("teacher", $where, $oredrBy, 0, 1);
        $data['teacher'] = $data['teacher'][0];
        $data['zhicheng'] = $this->teacher_m->getZhiCheng();
        $data['csrf_name'] = $this->security->get_csrf_token_name();
        $data['csrf_token'] = $this->security->get_csrf_hash();
        $this->load->view('teacher_edit', $data);
    }

    public function add() {
        $data['title'] = '增加教师风采';
        $data['zhicheng'] = $this->teacher_m->getZhiCheng();
        $data['csrf_name'] = $this->security->get_csrf_token_name();
        $data['csrf_token'] = $this->security->get_csrf_hash();
        $this->load->view('teacher_edit', $data);
    }

}
